package com.atguigu.service_ucneter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.service_ucneter.entity.UcenterMember;
import com.atguigu.service_ucneter.entity.vo.RegisterVo;
import com.atguigu.service_ucneter.mapper.UcenterMemberMapper;
import com.atguigu.service_ucneter.service.UcenterMemberService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-07-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wapper = new QueryWrapper<>();
        wapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wapper);
        //判断这个对象是否为空
        if(ucenterMember == null){//没有这个手机号
            throw new GuliException(20001,"登陆失败");
        }
        //判断密码
        //因为存储到数据库密码肯定是要加密的
        //把输入的密码先进行加密,然后再进行比较
        //加密方式,MD5
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登陆失败");
        }
        //判断用户是否禁用
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"登陆失败");
        }
        //登陆成功
        //生成token字符串,使用token工具类
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return token;
    }


    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();;

        //手机号和密码非空判断
        if( StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wapper = new QueryWrapper<>();
        wapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wapper);
        if(count > 0){
            throw new GuliException(20001,"注册失败");
        }

        //数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setAvatar("https://edu-teacher-202.oss-cn-beijing.aliyuncs.com/static/teacherAvatarImg/2020/04/15/avatar.png");
        baseMapper.insert(ucenterMember);

    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {



        return baseMapper.countRegisterDay(day);
    }


}
